using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class UILevelsPicker : MonoBehaviour, IBackAction
{
	[SerializeField]
	private UIWorldPanel worldPanelPrefab;

	[SerializeField]
	private RectTransform newLevelsSoonPrefab;

	[SerializeField]
	private Transform verticalLayoutTransform;

	[SerializeField]
	private RectTransform worldsScrollTransform;

	[SerializeField]
	private RectTransform movablePanel;

	[SerializeField]
	private float maxPanelOffset = -1280f;

	private int worldsInitialized;

	private List<UIWorldPanel> worldsPanels = new List<UIWorldPanel>();

	private RectTransform newLevelsSoonPanel;

	private CanvasGroup canvasGroup;

	private Canvas canvas;

	private bool initialized;

	private bool visible;

	private void Awake()
	{
		canvas = GetComponent<Canvas>();
		canvasGroup = GetComponentInChildren<CanvasGroup>();
		canvasGroup.alpha = 0f;
		canvasGroup.interactable = false;
		canvas.enabled = false;
		Object.DontDestroyOnLoad(base.gameObject);
		visible = false;
	}

	private void Start()
	{
		ProgressManager progressManager = Globals.instance.progressManager;
		for (int i = progressManager.GetMinWorldId(); i <= progressManager.GetMaxWorldId(); i++)
		{
			List<ILevelProgress> levelsProgress = progressManager.GetLevelsInWorlds(i).Cast<ILevelProgress>().ToList();
			UIWorldPanel uIWorldPanel = UnityEngine.Object.Instantiate(worldPanelPrefab, verticalLayoutTransform);
			uIWorldPanel.Initialize(i, levelsProgress);
			worldsPanels.Add(uIWorldPanel);
		}
		newLevelsSoonPanel = UnityEngine.Object.Instantiate(newLevelsSoonPrefab, verticalLayoutTransform);
		SetNewLevelsPanelState();
		initialized = true;
	}

	public void OnWorldChanged(int worldId)
	{
		if (initialized)
		{
			UIWorldPanel uIWorldPanel = worldsPanels.Find((UIWorldPanel x) => x.WorldId == worldId);
			if (uIWorldPanel != null)
			{
				uIWorldPanel.RecreatePanels(Globals.instance.progressManager.GetLevelsInWorlds(worldId).Cast<ILevelProgress>().ToList());
			}
			SetNewLevelsPanelState();
		}
	}

	private void SetNewLevelsPanelState()
	{
		Level lastUnlockedLevel = Globals.instance.progressManager.GetLastUnlockedLevel();
		int maxLevelId = Globals.instance.progressManager.GetMaxLevelId();
		if (lastUnlockedLevel.AbsoluteLevelId == maxLevelId && lastUnlockedLevel.PlayedThisLevel)
		{
			newLevelsSoonPanel.gameObject.SetActive(value: true);
		}
		else
		{
			newLevelsSoonPanel.gameObject.SetActive(value: false);
		}
	}

	public void OnWorldInitialized()
	{
		worldsInitialized++;
		if (worldsInitialized == worldsPanels.Count)
		{
			StartCoroutine(InitializeVerticalLayout());
		}
	}

	private IEnumerator InitializeVerticalLayout()
	{
		WaitForEndOfFrame waitFor = new WaitForEndOfFrame();
		yield return waitFor;
		verticalLayoutTransform.GetComponentInParent<VerticalLayoutGroup>().enabled = true;
		yield return waitFor;
		verticalLayoutTransform.GetComponentInParent<ContentSizeFitter>().enabled = true;
		foreach (UIWorldPanel worldsPanel in worldsPanels)
		{
			worldsPanel.OnPanelInVerticalLayout();
		}
		yield return waitFor;
		SetChildsState();
		Vector2 sizeDelta = worldsPanels[0].GetComponent<RectTransform>().sizeDelta;
		float worldPanelSizeY = sizeDelta.y;
		float num = worldPanelSizeY;
		Vector2 sizeDelta2 = worldsScrollTransform.sizeDelta;
		if (num < sizeDelta2.y)
		{
			Vector2 sizeDelta3 = worldsScrollTransform.sizeDelta;
			float f = sizeDelta3.y - worldPanelSizeY;
			verticalLayoutTransform.GetComponent<VerticalLayoutGroup>().padding.bottom = Mathf.RoundToInt(f);
			//verticalLayoutTransform.GetComponent<VerticalLayoutGroup>().spacing = f;
		}
	}

	private void SetChildsState(bool state = false)
	{
		for (int i = 0; i < base.transform.childCount; i++)
		{
			base.transform.GetChild(i).gameObject.SetActive(state);
		}
	}

	[ContextMenu("Show")]
	public void Show()
	{
		if (!visible)
		{
			SetChildsState(state: true);
			canvas.enabled = true;
			canvasGroup.alpha = 0f;
			canvasGroup.interactable = true;
			RectTransform rectTransform = movablePanel;
			Vector2 anchoredPosition = movablePanel.anchoredPosition;
			rectTransform.anchoredPosition = new Vector2(anchoredPosition.x, maxPanelOffset);
			iTween.ValueTo(base.gameObject, iTween.Hash("from", 0, "to", 1, "time", 0.3f, "onupdate", "ShowStep"));
			Globals.instance.backManager.RegisterObject(this);
			visible = true;
		}
	}

	[ContextMenu("Hide")]
	public void Hide()
	{
		if (visible && canvasGroup.interactable)
		{
			canvasGroup.interactable = false;
			iTween.ValueTo(base.gameObject, iTween.Hash("from", 1, "to", 0, "time", 0.3f, "onupdate", "ShowStep", "oncomplete", "OnHideEnd"));
			visible = false;
		}
	}

	private void OnHideEnd()
	{
		canvas.enabled = false;
		SetChildsState();
		Globals.instance.backManager.UnregisterObject(this);
	}

	public void ShowStep(float value)
	{
		canvasGroup.alpha = value;
		RectTransform rectTransform = movablePanel;
		Vector2 anchoredPosition = movablePanel.anchoredPosition;
		rectTransform.anchoredPosition = new Vector2(anchoredPosition.x, maxPanelOffset * (1f - value));
	}

	public void OnBackAction()
	{
		AudioController.instance.Play("button");
		Hide();
	}
}
